/**
 * 
 */

;(function($){ 
	//初始化变量
	var newguideKey = 'newguides';
	var newguideDiv = '<div class="New_user_lead">'
        +'<div class="Content_user_lead"></div>'
        +'<div class="Del_user_lead"></div>'
        +'<div class="Rack_user_lead"></div>'
        +'</div>';
	
	function Newguide(el, options) {
		this.el = $(el);
		var defaults = {
        	name:'',
        	css:{"position":"relative"},
    		content:'',
    		contentCss:'',
    		contentClass:'',
    		delCss:'',
    		delClass:'',
    		rackCss:'',
    		rackClass:'',
    		newguideCss: '',
    		newguideClass:'',
    		onCloseSuccess:null,
    		isShow:true
        };
        this.options = $.extend(defaults, options); 
        this.initialize();
        return this;
    }
	
/*    $.fn.newguide = function(options) {
    	var elem = $(this);
    	return new Newguide(elem, options);
    };*/

    Newguide.prototype = {
    	createContent : function(me,newguidestr){
    		var uid,newGuideElId;
    		var options = me.options;
    		var name = options.name;
    		
    		if(newguidestr != 'undefined' && newguidestr != null 
    				&& newguidestr != '' && newguidestr.indexOf(name) != -1){
				return ;
			}
    		
    		uid = Math.floor(Math.random() * 0x100000).toString(16);
    		newGuideElId = 'Newguide_' + uid;
    		this.newGuideElId = newGuideElId;
    		
    		me.el.append(newguideDiv);
    		if(options.css != ''){
    			me.el.css(options.css);
    		}
    		
            var newGuideObj = me.el.find(".New_user_lead");
            newGuideObj.attr("id",newGuideElId);
            newGuideObj.attr("name",options.name);
            if(options.newguideCss != ''){
            	newGuideObj.css(options.newguideCss);
            }
            if(options.newguideClass != ''){
            	newGuideObj.addClass(options.newguideClass);
            }
            
            //添加引导内容及修改样式
            var contentObj = me.el.find('.Content_user_lead');
            if(options.contentCss != ''){
            	contentObj.css(options.contentCss);
            }
            if(options.contentClass != ''){
            	 contentObj.addClass(options.contentClass);
            }
            contentObj.html(options.content);
            
            //删除按钮
            var delObj = me.el.find('.Del_user_lead');
            if(options.delCss != ''){
            	delObj.css(options.delCss);
            }
            if(options.delClass != ''){
            	delObj.addClass(options.delClass);
            }
            delObj.on('click',function(e){
            	me.close(options,e);
            });
            
            //箭头
            var rackObj = me.el.find('.Rack_user_lead');
            if(options.rackCss != ''){
            	rackObj.css(options.rackCss);
            }
            if(options.rackClass != ''){
            	rackObj.addClass(options.rackClass);
            }
            
            if(options.isShow){
            	newGuideObj.show();
            }else{
            	newGuideObj.hide();
            }
            newGuidePVCount();
    	},
    	initialize : function(){
    		 var me = this;
    		 var newGuideStr = me.getGuidesToCookie();
    	     if(getSessionUserId() != "" && getSessionUserId() != null){
				$.ajax({
				    type: "GET",
				    url: weburl + "/settings/findNewGuide",
				    dataType:"json",
				    success: function(data) {
				    	if(data.rsCode == 1){
				    		me.createContent(me,data.guidance);
				    	}else{
				    		me.createContent(me,newGuideStr);
				    	}
					}
				});
    	    }else{
    	    	me.createContent(me,newGuideStr);
    	    }
    	},
    	close:function(options,e){
    		var me = this;
    		e.stopPropagation();
    		var name = options.name;
    		me.el.find(".New_user_lead").remove();
    		me.setGuideToCookie(name);
    		
    		if(options.onCloseSuccess != null && typeof options.onCloseSuccess == 'function'){
    			options.onCloseSuccess();
    		}
    		
    		if(getSessionUserId() != "" && getSessionUserId() != null){
    		   $.post(weburl + "/settings/saveNewGuide?guide="+name);
    		}
    	},
    	setGuidesToCookie :function(newguidestr){
    		var me = this;
    		if(newguidestr != null && newguidestr.length >0){
    			var guides = newguidestr.split(",");
    			for(var i = 0;i <guides.length; i ++){
    				me.setGuideToCookie(guides[i]);
    			}
    		}
    	},
    	setGuideToCookie:function(name){
    		if(name != null && name != undefined && name != ''){
    			var ck = $.cookie(newguideKey);
    			if(ck == null) {
    				ck = '';
    			}
    			
        		if(ck.length > 0){
        			var f = false;
        			var cs = ck.split(",");
    				var len = cs.length;
        			for(var i=0;i < len; i++){
        				if(name == cs[i]){
        					f = true;
        					break;
        				}
        			}
        			if(!f){
        				ck = ck + "," + name; 
        			}
        		}else{
        			ck = name;
        		}
        		$.cookie(newguideKey, ck, { expires:360 }); 
                console.log($.cookie(newguideKey));
    		}
    	},
    	getGuidesToCookie:function(){
    		var ck = $.cookie(newguideKey);
    		var newGuideStr = '';
    		if(ck != null && ck.length > 0){
    			var cs = ck.split(",");
				var len = cs.length;
    			for(var i = 0;i < len; i++){
    				newGuideStr = newGuideStr + cs[i] + ',';
    			}
    			return newGuideStr.substring(0,newGuideStr.length - 1);
    		}
    		return newGuideStr;
    	}
    };
    
    
    
    $.fn.extend({
    	newguide:function(options){
    		return this.each(function() {
    			var elem = $(this);
    	    	return new Newguide(elem, options);
    		});
    	},
    	hideGuide:function(){
    		this.each(function() {
    			$(this).find(".New_user_lead").hide();
    		});
    	},
    	showGuide:function(){
    		this.each(function() {
    			$(this).find(".New_user_lead").show();
    		});
    	}
    });
	
})(jQuery); 
